﻿using System;
using System.Windows;
using Ping.App;
using Ping.Client.ViewModels;
using Ping.Core;

namespace Ping.Client.Pages
{
	public partial class TraceResultPage
	{
		public TraceResultPage()
		{
			InitializeComponent();

			Loaded += PageLoaded;
		}

		private void PageLoaded(object sender, RoutedEventArgs e)
		{
			if (StateService.State.TraceResult is TracerouteResult)
			{
				var tmp = (TracerouteResult)StateService.State.TraceResult;
				var lastHop = tmp.Hops[tmp.Hops.Length - 1];

				tb_host.Text = lastHop.Sender.ToString();
				tb_status.Text = lastHop.Status.ToString();
				tb_rtt.Text = lastHop.Status == IpStatus.Success
				              	? string.Format("{0}ms", lastHop.Rtt.Milliseconds.ToString())
				              	: "n/a";

				lb_hops.ItemsSource = new TracerouteModel(tmp).Rows;
			}
		}

		private void MapClick(object sender, RoutedEventArgs e)
		{
			NavigationService.Navigate(new Uri("/Pages/MapResultPage.xaml", UriKind.Relative));
		}
	}
}